//
//  RegularExp.m
//  Simubang_iphone
//
//  Created by Kelly xu on 16/3/23.
//  Copyright © 2016年 Simubang. All rights reserved.
//

#import "RegularExp.h"

@implementation RegularExp
+(BOOL)isValidateStr:(NSString*)str type:(enum RegexType)type{
    switch (type) {
        case RegexEmail:/*邮箱验证 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" Obj:str];
            break;
        case RegexMobile:/*手机号码验证 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^1\\d{10}$" Obj:str];
            break;
        case RegexName:/*用户名验证 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^(?![0-9]+$)[0-9a-zA-Z\u4E00-\u9FA5]*$" Obj:str];
            break;
        case RegexZhengjian:/*证件验证 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^[A-Za-z0-9()]*$" Obj:str];
            break;
        case RegexCountryArea:/*国家区域验证 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^[0-9a-zA-Z\u4E00-\u9FA5]*$" Obj:str];
            break;
        case RegexCar:/*车牌号验证 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$" Obj:str];
            break;
        case RegexCustomerName:/*客户名称 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^[a-zA-Z0-9\u4E00-\u9FA5]*$" Obj:str];
            break;
        case RegexNational:/*民族 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^[\u4E00-\u9FA5]*$" Obj:str];
            break;
        case RegexNameEnglish:/*FamilyName等英文名 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^[A-Za-z]*$" Obj:str];
            break;
        case RegexNormal:/*名称验证 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^[a-zA-Z0-9\u4E00-\u9FA5]*$" Obj:str];
            break;
        case RegexPhone:/*座机号码验证 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^(0\\d{2,3}-\\d{7,8})*$" Obj:str];
            break;
        case RegexPostalCode:/*邮政编码验证 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^[0-9-]*$" Obj:str];
            break;
        case RegexConnectMan:/*紧急联系人验证 MODIFIED BY HELENSONG*/
            return [self ValidateWithRegex:@"^[a-zA-Z\u4E00-\u9FA5]*$" Obj:str];
            break;
        case RegexTrueName:
            return [self ValidateWithRegex:@"^[A-Za-z\u4E00-\u9FA5]*$" Obj:str];
            break;
        case RegexLoginName:
            return [self ValidateWithRegex:@"^[A-Za-z0-9_]*$" Obj:str];
            break;
        case RegexLoginPwd:
            return [self ValidateWithRegex:@"^[A-Za-z0-9]*$" Obj:str];
            break;
        case RegexPwd:
            return [self ValidateWithRegex:@"^(?![0-9_]+$)(?![a-zA-Z_]+$)[0-9A-Za-z_]*$" Obj:str];
            break;
        case RegexEnCn:
            return [self ValidateWithRegex:@"^[a-zA-Z\u4E00-\u9FA5]*$" Obj:str];
            break;
        case RegexEnCnNoDashes:
            return [self ValidateWithRegex:@"^[a-zA-Z0-9\u4E00-\u9FA5-]*$" Obj:str];
            break;
        case RegexSACode:
            return [self ValidateWithRegex:@"^[[A-Za-z]{2}\\d{2}(\\s\\d{4}){4}]*$" Obj:str];
            break;
        case RegexIDCard:
            return [self ValidateWithRegex:@"^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$" Obj:str];
            break;
        case RegexNumberAndSpell:
            return [self ValidateWithRegex:@"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]*$" Obj:str];
            break;
        case RegexEnNumberAndCn: // 字母数字和中文
            return [self ValidateWithRegex:@"^[a-zA-Z0-9\u4e00-\u9fa5]+$" Obj:str];
            break;
        default:
            break;
    }
    return NO;
}
+(BOOL)ValidateWithRegex:(NSString*)regex Obj:(NSString*)obj{
    NSString *Regex = regex;
    NSPredicate *Test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",Regex];
    return [Test evaluateWithObject:obj];
}

+ (BOOL)isValidateStr:(NSString *)str minLen:(NSInteger)minLen maxLen:(NSInteger)maxLen{
    NSAssert(maxLen >= minLen, @"区间输入有误");
    NSUInteger length = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    return length >= minLen && maxLen >= length;
}
+ (BOOL)isValidateStr:(NSString *)str type:(enum RegexType)type minLen:(NSInteger)minLen maxLen:(NSInteger)maxLen{
    BOOL validLen = [self isValidateStr:str minLen:minLen maxLen:maxLen];
    if (!validLen) {
        return NO;
    }
    validLen = [self isValidateStr:str type:type];
    return validLen;
}
@end
